package com.zy.explore.sys.interfaces.assembler;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zy.explore.common.infrastructure.util.DataBeanUtil;
import com.zy.explore.common.infrastructure.util.JwtUtil;
import com.zy.explore.sys.application.service.StationMsgService;
import com.zy.explore.sys.application.service.UserMsgService;
import com.zy.explore.sys.domain.mybatis.entity.StationMsg;
import com.zy.explore.sys.domain.mybatis.entity.SysUser;
import com.zy.explore.sys.domain.mybatis.entity.UserMsg;
import com.zy.explore.sys.interfaces.frm.StaMsgSaveFrm;
import com.zy.explore.sys.interfaces.frm.SysUserFrm;
import com.zy.explore.sys.interfaces.vo.SendToUserVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * description 站内信管理转换类
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021年07月11日 23:24
 */
@Component
public class StationMsgAssembler {

    @Resource
    private StationMsgService service;

    @Resource
    private UserMsgService userMsgService;

    @Transactional(rollbackFor = Throwable.class)
    public Boolean save(StaMsgSaveFrm frm) {
        StationMsg stationMsg = convertFromFrm(frm);
        service.insertSelective(stationMsg);
        userMsgService.sendMsg(stationMsg, frm.getUsers());
        return true;
    }

    /**
     * frm转实体类
     *
     * @param frm 参数对象
     * @return 转换后的实体类对象
     */
    public static StationMsg convertFromFrm(StaMsgSaveFrm frm) {
        StationMsg stationMsg = DataBeanUtil.copyOf(frm, StationMsg.class, true);
        if (!CollectionUtils.isEmpty(frm.getUsers())) {
            stationMsg.setSendUsers(JSON.toJSONString(frm.getUsers()));
        }
        Date current = new Date();
        if (!StringUtils.isEmpty(frm.getPublishTime())) {
            DateTime publishTime = DateUtil.parse(frm.getPublishTime(), DateUtils.DATE_FORMAT_19);
            if (publishTime.getTime() < System.currentTimeMillis()) {
                stationMsg.setPublishTime(current);
            } else {
                stationMsg.setPublishTime(publishTime);
            }
        }
        stationMsg.setCreatorId(JwtUtil.getCurrentUserId());
        return stationMsg;
    }

    /**
     * 查询发送对象列表集合
     *
     * @return {@link List<SendToUserVO>}
     */
    public List<SendToUserVO> getSendUserList() {
        List<SendToUserVO> retList = Lists.newArrayList();
        List<SysUser> userList = userMsgService.getSendUserList();
        if (CollectionUtils.isEmpty(userList)) {
            return retList;
        }
        userList.forEach(user -> retList.add(SendToUserVO.builder().id(user.getId()).name(user.getName()).build()));
        return retList;
    }
}
